package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.ShoesListMapper;
import com.manage.mapper.ShoesMapper;
import com.manage.pojo.Shoes;
import com.manage.pojo.operation.ShoesList;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class ShoesServiceImpl implements ShoesService{

    @Autowired
    private ShoesMapper shoesMapper;
    @Autowired
    private ShoesListMapper shoesListMapper;
    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveShoes(Shoes shoes) {
        String shoesId = sid.nextShort();
        shoes.setShoesId(shoesId);
        shoesMapper.insert(shoes);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryShoesIsExit(String shoesId) {
        Shoes shoes = new Shoes();
        shoes.setShoesId(shoesId);
        Shoes result = shoesMapper.selectOne(shoes);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void shoesDelete(String shoesId) {
        Example shoesExampe = new Example(Shoes.class);
        Criteria criteria = shoesExampe.createCriteria();
        criteria.andEqualTo("shoesId", shoesId);
        shoesMapper.deleteByExample(shoesExampe);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void updateShoesInfo(Shoes shoes) {
        Example shoesExampe = new Example(Shoes.class);
        Criteria criteria = shoesExampe.createCriteria();
        criteria.andEqualTo("shoesId", shoes.getShoesId());
        shoesMapper.updateByExampleSelective(shoes, shoesExampe);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ShoesList queryShoesByShoesId(String shoesId) {
        ShoesList result = shoesListMapper.queryShoesByShoesId(shoesId);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryShoesByShoesType(Integer page, Integer pageSize, String shoesType) {
        PageHelper.startPage(page, pageSize);
        List<ShoesList> list = shoesListMapper.queryShoesByShoesType(shoesType);
        PageInfo<ShoesList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryShoesByUserId(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page, pageSize);
        List<ShoesList> list = shoesListMapper.queryShoesByUserId(userId);
        PageInfo<ShoesList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryAllShoes(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ShoesList> list = shoesListMapper.queryAllShoes();
        PageInfo<ShoesList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
